

import { forwardRef, useImperativeHandle, useRef, useState } from "react";
import { DevGrid, DevPopup } from "~/public/cpas-ui";
import { NonStatSampleParameter } from "../../../typing";
import { consts } from "../../../consts";
import { getRiskCoefficientColumns, guaranteeCoefficientDataSource } from "./GuaranteeCoefficientService";
import { msgError } from "@/utils/msg";
import { getCpasPrimaryColor } from "@/utils/pub";


export const GuaranteeCoefficientPopup = forwardRef((props: {
    nonStatSampleParameter?: NonStatSampleParameter,
    setNonStatSampleParameter?: (value: NonStatSampleParameter) => void
}, ref) => {

    const [visible, setVisible] = useState<boolean>(false);
    const diaParams = useRef<any>();
    const dataGridRef = useRef<any>();
    // const guaranteeCoefficientRef = useRef<GuaranteeCoefficient>({ ...props.nonStatSampleParameter?.guaranteeCoefficient || {} });

    // const [guaranteeCoefficient, setGuaranteeCoefficient] = useState<GuaranteeCoefficient>(props.guaranteeCoefficient || {});
    // 自定义方法
    useImperativeHandle(ref, () => ({
        show: (params: any) => {
            diaParams.current = params;
            setVisible(true);
        }
    }))
    function onCancel() {
        setVisible(false)
        if (diaParams.current.setIsNeedGuaranteeCoefficientPopup) {
            diaParams.current.setIsNeedGuaranteeCoefficientPopup(false);
        }
    }
    function onOKClick() {
        //只查看

        /* if (!props.nonStatSampleParameter?.guaranteeCoefficient) {
            return msgError('还没有保证系数,请根据职业判断选择风险级别后再查看保证系数!')
        }
        if ((props.setNonStatSampleParameter && props.nonStatSampleParameter)) {
            props.setNonStatSampleParameter({
                ...props.nonStatSampleParameter,
                guaranteeCoefficient: { ...guaranteeCoefficientRef.current }
            })
        }
        onCancel(); */
    }
    return <>
        <DevPopup.Logic
            visible={visible}
            title='保证系数表'
            width={1024}
            height={400}
            onHiding={onCancel}
            onCancel={onCancel}
            onOK={onOKClick}
            toolbarDisplay={false}
        >{() => {//必须用函数返回

            const majorMisstatementRiskText = guaranteeCoefficientDataSource.find(
                item => item.rowName === props.nonStatSampleParameter?.majorMisstatementRisk)?.rowText

            const columns = getRiskCoefficientColumns();
            const subColumns = columns.find(item => item.columns)?.columns;
            const otherSubstantiveRiskText = subColumns?.find((item: any) => item.dataField
                == props.nonStatSampleParameter?.otherSubstantiveRisk)?.caption;
            return (<>
                <div style={{ marginBottom: 10, color: getCpasPrimaryColor() }}>
                    注：使用下列公式估计样本规模:样本规模=抽样总体账面金额÷可容忍错报X保证系数,根据上述公式计算的样本规模是在预计没有错报时细节测试的最低样本数
                </div>
                <DevGrid.RangeTap
                    ref={dataGridRef}
                    className='devHeadersCenter PaperDevSortableGrid advancedQueryDataGrid'
                    dataSource={guaranteeCoefficientDataSource}
                    height={210}
                    keyExpr="key"
                    isRemote={true}
                    showBorders={false}
                    showRowLines={true}
                    showColumnLines={true}
                    rowAlternationEnabled={true}
                    columns={columns}
                    paging={{ enabled: false }}
                    scrolling={{ showScrollbar: 'never' }}
                    onCellClick={(e) => {
                        /*  if (guaranteeCoefficientDataSource.map(item => item.rowName).includes(e.column.dataField || '')) {
                             guaranteeCoefficientRef.current = {
                                 significantMissValue: e.value,
                                 significantMissColName: e.column.dataField,
                                 significantMissRowName: e.data.rowName,//重点错报行名 highest/high/middle/low
                             }
                         } */
                    }}
                    onCellDblClick={(e) => {
                        /*  if (guaranteeCoefficientDataSource.map(item => item.rowName).includes(e.column.dataField || '')) {
                             onOKClick()
                         } else {
                             msgError('此列双击数据无效!')
                         } */
                    }}
                />
                <div style={{ display: 'flex', alignContent: 'left', marginTop: 10, color: getCpasPrimaryColor() }}>
                    <div >
                        重大错报风险水平: {majorMisstatementRiskText ? `【${majorMisstatementRiskText}】` : consts.curNoValue}
                        其他实质性风险: {otherSubstantiveRiskText ? `【${otherSubstantiveRiskText}】` : consts.curNoValue}
                        对应值: {props.nonStatSampleParameter?.guaranteeCoefficient || consts.curNoValue}</div>
                    {/*这上会导致表格闪烁  <div style={{ marginLeft: 20 }}>选中值: {guaranteeCoefficient.significantMissValue || consts.curNoValue}</div> */}
                </div>
            </>)
        }}
        </DevPopup.Logic >
    </>
});
